﻿$(document).ready(function () {
    $("body").css({
        "color": "white",
        "background": "transparent"
    });

    $("#btnSave").click(handleSave);

    $("#btnClose").click(function () {
        var w = getTopWindow();
        if (typeof w.disablePopup === "function") {
            w.disablePopup();
        }
    });
});

var handleSave = function (e) {
    var parentGroup = $("#parentGroup").val();
    var mode = $("#mode").val();
    var groupName = $("#groupName").val();
    var url = "/Master/RegisterGroup";
    var data = {
        groupName: groupName,
        mode: mode
    };

    if (mode == UPDATE_SUB_GROUP || mode == UPDATE_GROUP) {
        url = "/Master/UpdateGroup";
        data.id = $("#id").val();
    }
    else {
        data.parentGroup = parentGroup;
    }

    $.ajax({
        type: "POST",
        url: url,
        async: false,
        data: data,
        dataType: "json",
        success: function (response) {
            if (response == null || response == ERROR_PAGE) {
                alert("Something wrong was happened");
            }
            else if (response == LOST_SESSION) {
                redirectToLogin();
            }
            else {
                reloadCombo(response, groupName, mode);
            }
        }
    });
};

function reloadCombo(response, groupName, mode) {
    var w = getTopWindow();
    if (typeof w.refreshPage === "function") {
        w.refreshPage(response, groupName, mode);
    }
    if (typeof w.disablePopup === "function") {
        w.disablePopup();
    }
}